package com.pi.pro.xcmg.am.was.fac.service;


import com.pi.common.utils.StringUtils;
import com.pi.pro.xcmg.am.common.XcmgAmConstants;
import com.pi.pro.xcmg.am.common.vo.edi.LedMessageData;
import com.pi.wms.entity.TblLedMessage;
import com.pi.wms.service.LedMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



/**
 *
 */
@Slf4j
@Service
public class LedCustomDatatableWorkService {


    @Autowired
    private LedMessageService ledMessageService;


    //-----------------------------------------------------------------------
    //
    //-----------------------------------------------------------------------

    /**
     *  显示: 拣选口显示默认信息
     * */
    public String showPickStationDefText(Long divsionId, String ledCode){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        ledCode=ledCode.trim();
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode);
            if (led==null){
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_OUTBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PK_STATION_DEF_INFO);
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 显示: 拣选口显示拣选信息
     */
    public String showPickStationPickdata(Long divsionId, LedMessageData ledMessageData){
        String m = "";
        if (ledMessageData==null){
            return m;
        }
        if (StringUtils.isNullOrBlank(ledMessageData.getCode())){
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledMessageData.getCode());
            if (led==null) {
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_OUTBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PK_STATION_PICK_DATA);
            this.makePickdata(ledMessageData, led);
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 显示: 拣选失败
     */
    public String showPickStationPickdError(Long divsionId, String ledCode, String errorText){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            m="请指定LED屏.";
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        if (StringUtils.isNullOrBlank(errorText)){
            errorText="拣选失败";
        }
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode.trim());
            if (led==null) {
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_OUTBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PK_STATION_PICK_ERROR);
            led.setMessage(errorText.trim());
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 显示: 拣选成功
     */
    public String showPickStationPickdSuccess(Long divsionId, String ledCode, String text){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            m="请指定LED屏.";
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        if (StringUtils.isNullOrBlank(text)){
            text="拣选成功";
        }
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode.trim());
            if (led==null) {
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_OUTBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PK_STATION_PICK_OK);
            led.setMessage(text.trim());
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }


    /**
     *  显示: 入库口显示默认信息
     * */
    public String showPutawayStationDefText(Long divsionId, String ledCode){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        ledCode=ledCode.trim();
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode);
            if (led==null){
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_INBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PA_STATION_DEF_INFO);
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 显示: 入库失败
     */
    public String showPutawayStationExecuteError(Long divsionId, String ledCode, String errorText){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            m="请指定LED屏.";
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        if (StringUtils.isNullOrBlank(errorText)){
            errorText="上架失败";
        }
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode.trim());
            if (led==null) {
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_INBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PA_STATION_PA_ERROR);
            led.setMessage(errorText.trim());
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    /**
     * 显示: 入库成功
     */
    public String showPutawayStationExecuteSuccess(Long divsionId, String ledCode, String text){
        String m = "";
        if (StringUtils.isNullOrBlank(ledCode)){
            m="请指定LED屏.";
            return m;
        }
        if (divsionId==null){
            divsionId=XcmgAmConstants.DIVISION_WH101_ID;
        }
        if (StringUtils.isNullOrBlank(text)){
            text="拣选成功";
        }
        TblLedMessage led=null;
        try{
            led=this.ledMessageService.findByCode(divsionId, ledCode.trim());
            if (led==null) {
                return m;
            }
            this.cleanPickdata(led);
            led.setType(XcmgAmConstants.LED_ACTION_TYPE_INBOUND);
            led.setBizType(XcmgAmConstants.LED_BIZ_TYPE_PA_STATION_PA_ERROR);
            led.setMessage(text.trim());
            this.switchStatus(led);
            this.ledMessageService.save(led);
        }catch (Exception e){
            e.printStackTrace();
        }
        return m;
    }

    private void switchStatus(TblLedMessage led){
        if (led==null){
            return;
        }
        if (XcmgAmConstants.LED_STATUS_CODE_1.equalsIgnoreCase(led.getStatus())){
            led.setStatus(XcmgAmConstants.LED_STATUS_CODE_2);
        }else{
            led.setStatus(XcmgAmConstants.LED_STATUS_CODE_1);
        }
    }


    private void makePickdata(LedMessageData source, TblLedMessage target){
        if (source==null){
            return;
        }
        if (target==null){
            return;
        }
        target.setPalletCode(source.getPalletCode());
        target.setTaskCode(source.getTaskCode());
        target.setMatCode(source.getMessage());
        target.setMessage(source.getMessage());
        target.setQuantity(source.getQuantity());
        target.setUnit(source.getUnit());
        target.setLineCode(source.getLineCode());
        target.setStation(source.getStation());
        target.setStocktaking(source.getStocktaking());
    }

    private void cleanPickdata(TblLedMessage led){
        if (led==null){
            return;
        }
        //led.setType(null);
        //target.setCode(null);
        //target.setIp(null);
        //led.setBizType(null);
        led.setPalletCode(null);
        led.setTaskCode(null);
        led.setMatCode(null);
        led.setMessage(null);
        led.setQuantity(null);
        led.setUnit(null);
        led.setLineCode(null);
        led.setStation(null);
        led.setStocktaking(null);
        //led.setDf1(null);
        //led.setDf2(null);
        //led.setDf3(null);
        //led.setDf4(null);
    }


}
